PKGNAME = @PROJECTNAME@
TARGET = @PROJECTNAME@
MODULE = $(TARGET).la

# Various configurable paths (remember to edit Makefile.in, not Makefile)
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
includedir = @includedir@
libdir =  @libdir@
mandir = @mandir@
libtool = @libtool@
sysconfdir = @sysconfdir@
tls = @tls@
NETOPEER_DIR = @NETOPEER_DIR@

CC = @CC@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
LIBS = @LIBS@
CFLAGS = -Wall -Wno-unused-result @CFLAGS@
CPPFLAGS = @CPPFLAGS@ -DNETOPEER_DIR=\"$(NETOPEER_DIR)\"
LIBTOOL = $(libtool) --tag=CC --quiet
NETOPEER_MANAGER = @NETOPEER_MANAGER@

MODEL = model/ietf-system.yin \
	model/iana-crypt-hash.yin \
	model/ietf-system-data.rng \
	model/ietf-system-gdefs.rng \
	model/ietf-system-schematron.xsl

ifeq ($(tls), yes)
	MODEL := $(MODEL) model/ietf-system-tls-auth.yin \
		model/ietf-x509-cert-to-name.yin
endif

PYMODULES = nc_cfgsystem

SRCS = $(TARGET).c \
	base/common.c \
	base/date_time.c \
	base/platform.c \
	base/dns_resolver.c \
	base/shutdown.c \
	base/encrypt.c \
	base/salt.c \
	base/local_users.c

ifeq ($(tls), yes)
	SRCS := $(SRCS) base/cert.c
endif

OBJDIR = .obj
LOBJS = $(SRCS:%.c=$(OBJDIR)/%.lo)

define PYINSTALL
cd $(1); \
python setup.py install &>/dev/null; \
cd -;
endef

all: $(MODULE) $(TARGET)-init

$(TARGET)-init: $(SRCS) $(TARGET)-init.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $^ $(LIBS)

$(MODULE): $(LOBJS)
	$(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(CPPFLAGS) $(LIBS) -avoid-version -module -shared -export-dynamic --mode=link -o $@ $^ -rpath $(libdir)

$(OBJDIR)/%.lo: %.c
	@[ -d $$(dirname $@) ] || \
		(mkdir -p $$(dirname $@))
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -shared -c $< -o $@

.PHONY: install
install: $(MODULE) $(TARGET)-init
	$(INSTALL) -m 775 -d $(DESTDIR)/$(libdir)
	$(LIBTOOL) --mode=install cp $(MODULE) $(DESTDIR)/$(libdir)/;
	$(INSTALL) -d $(NETOPEER_DIR)/ietf-system/
	@for i in $(MODEL); do \
		$(INSTALL_DATA) -m 600 $$i $(NETOPEER_DIR)/ietf-system/; \
	done
	if test -n "$(NETOPEER_MANAGER)"; then \
		if test -n "`$(NETOPEER_MANAGER) list | grep "^ietf-system ("`"; then \
			$(NETOPEER_MANAGER) rm --name ietf-system; \
		fi; \
		$(NETOPEER_MANAGER) add --name ietf-system \
			--model $(NETOPEER_DIR)/ietf-system/ietf-system.yin \
			--transapi $(DESTDIR)/$(libdir)/cfgsystem.so \
			--features @FEATURES@ \
			--datastore $(NETOPEER_DIR)/ietf-system/datastore.xml; \
		if test "$(tls)" = "yes"; then \
			cp ./datastore.xml $(NETOPEER_DIR)/ietf-system/datastore.xml; \
			chmod og-rwx $(NETOPEER_DIR)/ietf-system/datastore.xml; \
			$(NETOPEER_MANAGER) add --name ietf-system \
				--import $(NETOPEER_DIR)/ietf-system/ietf-x509-cert-to-name.yin; \
			$(NETOPEER_MANAGER) add --name ietf-system \
				--augment $(NETOPEER_DIR)/ietf-system/ietf-system-tls-auth.yin \
				--features tls-map-certificates; \
		fi; \
	fi
	if test "$(tls)" = "yes"; then \
		export STUNNEL_CA_PATH=`sed -n 's/^CApath *= *\(.*\)/\1/p' $(NETOPEER_DIR)/stunnel_config`; \
			./$(TARGET)-init $(NETOPEER_DIR)/ietf-system/datastore.xml @FEATURES@ tls-map-certificates; \
	else \
		./$(TARGET)-init $(NETOPEER_DIR)/ietf-system/datastore.xml @FEATURES@; \
	fi
	$(foreach module,$(PYMODULES),$(call PYINSTALL,$(module)))

.PHONY: uninstall
uninstall:
	$(LIBTOOL) --mode=uninstall rm -rf $(DESTDIR)/$(libdir)/$(MODULE);
	rm -rf $(NETOPEER_DIR)/ietf-system/
	if test -n "$(NETOPEER_MANAGER)"; then \
		if test -n "`$(NETOPEER_MANAGER) list | grep "^ietf-system ("`"; then \
			$(NETOPEER_MANAGER) rm --name ietf-system; \
		fi; \
	fi

.PHONY: clean
clean:
	$(LIBTOOL) --mode clean rm -f $(LOBJS)
	$(LIBTOOL) --mode clean rm -f $(MODULE)
	rm -rf $(MODULE) $(TARGET)-init $(OBJDIR)

